package cn.lyjuan.dwz_manage.core.java.service;


import cn.lyjuan.dwz_manage.core.base.pojo.ActionPojo;
import cn.lyjuan.dwz_manage.core.base.pojo.ManagePojo;
import cn.lyjuan.dwz_manage.core.java.dto.GroupActionBean;
import cn.lyjuan.dwz_manage.core.java.dto.QueryResultBean;
import cn.lyjuan.dwz_manage.core.java.exception.DwzManageException;
import cn.lyjuan.dwz_manage.core.java.mapper.IMgActionGroupMapper;
import cn.lyjuan.dwz_manage.core.java.mapper.IMgActionMapper;
import cn.lyjuan.dwz_manage.core.java.util.SuperAdminUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by ly on 2015/1/11.
 */
@Service("mgActionService")
public class ActionService
{
    @Autowired(required = false)
    private IMgActionGroupMapper actionGroupMapper;

    @Autowired(required = false)
    private IMgActionMapper actionMapper;

    /**
     * 查询登陆用户可用权限
     * @param manage        当前登陆用户编号
     * @param onlyNav       仅显示导航请求
     * @return
     */
    public List<GroupActionBean> queryActions(ManagePojo manage, boolean onlyNav)
    {
        if (SuperAdminUtils.isSuperManager(manage.getCreateby())) // 超级管理员，能查看所有权限
            return actionGroupMapper.selectAllActions(onlyNav);

        return actionGroupMapper.selectActionsForManage(manage.getId(), onlyNav);
    }

    /**
     * 分页检索查询请求
     * @param param
     * @param pn
     * @param ps
     * @return
     */
    public QueryResultBean<ActionPojo> queryActions(ManagePojo manage, Map<String, String> param, int pn, int ps)
    {
        QueryResultBean<ActionPojo> bean = new QueryResultBean<>();
        List<ActionPojo> list = null;
        int totalCount = -1;

        if (SuperAdminUtils.isSuperManager(manage.getCreateby()))
        {
            list = actionMapper.selectActions(param, (pn - 1) * ps, ps);
            totalCount = actionMapper.selectActionCount(param);
        } else
        {
            list = actionMapper.selectActionsForManage(manage.getId(), param, (pn - 1) * ps, ps);
            totalCount = actionMapper.selectActionCountForManage(manage.getId(), param);
        }

        bean.setTotalCount(totalCount);
        bean.setList(list);

        return bean;
    }

    /**
     * 角色已有的请求
     * @param roleid
     * @return
     */
    public List<ActionPojo> queryActionsForRole(int roleid)
    {
        return actionMapper.selectActionsForRole(roleid);
    }

    /**
     * 添加请求
     * @param action
     */
    public void addAction(ActionPojo action)
    {
        // 请求地址是否重复
        ActionPojo actionByUrl = actionMapper.selectByUrl(action.getActionurl());

        if (null != actionByUrl)
            throw new DwzManageException("请求地址已存在");

        actionMapper.insert(action);
    }

    /**
     * 根据编号查询请求信息
     * @param actionid
     * @return
     */
    public ActionPojo queryActionById(int actionid)
    {
        return actionMapper.selectById(actionid);
    }

    /**
     * 更新请求
     * @param action
     */
    public void updateAction(ActionPojo action)
    {
        actionMapper.updateById(action);
    }

    /**
     * 锁定请求
     * @param id
     * @param status {@link cn.lyjuan.dwz_manage.core.java.cst.TypeCst.Status}
     */
    public void updateStatus(int id, int status)
    {
        actionMapper.updateStatusById(id, status);
    }
}
